
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
/**Using*/
using System;
using System.Data;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.DataAccess;
using Swaf.DataAccess.AdoNet;
using Swaf.Security;
/**Using*/

public class /**$ClassName*/BizObjFormatterTemplate/**$ClassName*/ : AdoNetFormatter
{
	/**ChildNodes*/
	protected NodeInfoDictionary m_childNodes = new NodeInfoDictionary();
	protected override NodeInfoDictionary ChildNodes { get { return m_childNodes; } }/**ChildNodes*/
	/**ParentNode*/
	/**Type*/AdoNetFormatter/**Type*/ m_parent;/**ParentNode*/
	/**BizObjListDek*/
	IBizObjList m_list;
	/**BizObjListDek*/
	/**MergeBizObjListDek*/
	IKeyedBizObjList m_list;
	/**MergeBizObjListDek*/
	/**CustomDek*/
	/**CustomDek*/

	public /**$ClassName*/BizObjFormatterTemplate/**$ClassName*/(/**ConstructorParam*//**Type*/AdoNetFormatter/**Type*/ parent/**ConstructorParam*/)
	{
		/**Constructor*/
		base.m_bizObjType = "{0}";
		/**Constructor*/
		/**AddChildNode*/
		m_childNodes["{0}"] = new {1};
		/**AddChildNode*/
		/**SetupParam*/
		m_parent = parent;
		/**SetupParam*/
	}

	public object format(INameAddressSupport info, IDataReader reader)
	{
		int maxRows = -1;
		if(info.containsField("_maxRows"))
			maxRows = (int)info.get("_maxRows");
		bool doneOne = false;
		/**formatBizObjList*/
		/**CreateListTypeCustomCode*/
		m_list = App.bizObjMgr.createList("{0}");
		/**CreateListTypeCustomCode*/
		if (m_bizObjDefinition == null)
			m_bizObjDefinition = m_list.objectType;
		/**formatBizObjList*/
		/**formatBizObjListMerge*/
		IBizObjList list = info.get("{0}") as IBizObjList;
		m_list = list.getIndex(list.objectType.KeyFieldNames);
		if (m_bizObjDefinition == null)
			m_bizObjDefinition = m_list.objectType;
		/**formatBizObjListMerge*/
		/**formatBizObjMerge*/
		m_currentBizObj = info.get("{0}") as IBizObj;
		if (m_bizObjDefinition == null)
			m_bizObjDefinition = m_currentBizObj.type;
		/**formatBizObjMerge*/

		int recordsProcessed = 0;
		while(reader.Read())
		{
			if(!doneOne)
			{
				mapFieldsFromDataReader(reader.GetSchemaTable());
				/**formatBizObj*/
				m_currentBizObj = createBizObj(info,reader);
				if (m_bizObjDefinition == null)
					m_bizObjDefinition = m_currentBizObj.type;
				/**formatBizObj*/
				doneOne = true;
			}
			if(!populateBizObj(reader, false, info))
				break;
			recordsProcessed++;
			if(maxRows > 0 && recordsProcessed >= maxRows)
				break;
		/**formatBizObj2*/
		//break;
		/**formatBizObj2*/

		}
		
		/**FormatReturnObj*/
		return recordsProcessed > 0?m_currentBizObj:null;
		/**FormatReturnObj*/
		/**FormatReturnList*/
		return m_list;
		/**FormatReturnList*/
	}

	protected override bool RequiresParentName { get{return /**RequiresParentName*/{0}/**RequiresParentName*/;}}

	public override bool populateBizObj(IDataReader reader, bool parentKeyChanged, INameAddressSupport info)
	{
		if(m_dataReaderFields.Count == 0)
			return true;
		IComparable currentNodeKey = makeNodeKey(reader);
		bool changed = (base.LastNodeKey == null || parentKeyChanged || (currentNodeKey != null && currentNodeKey.CompareTo(base.LastNodeKey) != 0));
		if(changed)
		{
			/**getBizObjObj*/
			/**HasParent*/
			if(m_parent != null)
				m_currentBizObj = m_parent.CurrentBizObj.get("{0}") as IBizObj;
			/**HasParent*/
			/**getBizObjObj*/

			/**PreList*/
			if(currentNodeKey != null) 
			{
			/**PreList*/
			base.LastNodeKey = currentNodeKey;
			/**getBizObjMerge*/
			m_currentBizObj = m_list.find(currentNodeKey, false);
			/**getBizObjMerge*/
			/**getBizObjList*/
			/**setupList*/
			m_list = m_parent.CurrentBizObj.get("{0}") as IBizObjList;
			/**setupList*/
			m_currentBizObj = createBizObj(info,reader);
			/**getBizObjList*/
			processFields(reader);				
			/**PostList*/
			}
			/**PostList*/

		}
		if(m_currentBizObj != null)
			foreach(AdoNetFormatter child in ChildNodes.Values)
				child.populateBizObj(reader, changed, info);

		/**CustomBizObjProcessing*/
		if(changed)
		{
			/**Code*//**Code*/
		}
		/**CustomBizObjProcessing*/

		/**getBizObjList2*/
		if(changed && m_currentBizObj != null)
			m_list.Add(m_currentBizObj);			
		/**getBizObjList2*/
		if(changed && m_currentBizObj != null)
		{
			m_currentBizObj.clearDirtyFlags();
			m_currentBizObj.isNew=false;
		}
		return true;
	}

	protected IComparable makeNodeKey(IDataReader reader)
	{
		/**SingleFieldKey*/
		object val = reader.GetValue(getField("{0}").ReaderIndex);
		return (val is System.DBNull) ? null : (IComparable)BaseBizObj.BizObjFieldFactory.createFromNickName(getField("{0}").FieldType.FactoryType, val);
		/**SingleFieldKey*/

		/**MultiFieldKeyPre*/
		CompositeComparable compositeComparable = new CompositeComparable(true);
		bool keysAllNull = true;
		object keyObj;
		/**MultiFieldKeyPre*/

		/**MultiFieldKey*/
		keyObj = reader.GetValue(getField("{0}").ReaderIndex);
		if (!(keyObj is System.DBNull) && (keyObj != null))
			keysAllNull = false;
		compositeComparable.Add(((keyObj is System.DBNull) ? null : keyObj));
		/**MultiFieldKey*/

		/**MultiFieldKeyPost*/
		return keysAllNull ? null : compositeComparable;
		/**MultiFieldKeyPost*/
	}

	protected override string UpperName { get { return /**UpperName*/"{0}"/**UpperName*/; } }
	protected IBizObjDefinition m_bizObjDefinition = null;
	protected override IBizObjDefinition BizObjType
	{
		get
		{
			if(m_bizObjDefinition == null)
				m_bizObjDefinition = App.bizObjMgr.getDefinition(/**BizObjTypeName*/"{0}"/**BizObjTypeName*/);
			return m_bizObjDefinition;
		}
	}
	/**ReaderColNames*/
	protected BizObjFieldDictionary m_readerColNames = new BizObjFieldDictionary();
	protected override BizObjFieldDictionary ReaderColNameMappings 
	{
		get
		{
			if (m_readerColNames.Count == 0)
			{
				/**Name*/
				m_readerColNames["{0}"] = BizObjType.getField("{1}", false);
				/**Name*/
			}
			return m_readerColNames;
		} 
	}
	/**ReaderColNames*/

	protected void customProcessBizObj(IBizObj obj, IDataReader reader)
	{
		/**customProcessBizObj*/
		/**customProcessBizObj*/
	}

	protected IBizObj createBizObj(INameAddressSupport info, IDataReader reader)
	{
		IBizObj obj = null;
		/**CreateHomoType*/
		obj = App.bizObjMgr.create("{0}");
		/**CreateHomoType*/
		/**CreateTypeFromField*/
		obj = App.bizObjMgr.create(reader.GetString(getField("{0}").ReaderIndex));
		/**CreateTypeFromField*/
		/**CreateTypeCustomCode*/
		/**CreateTypeCustomCode*/
		return obj;
	}

	/**ChildNodeInfoTypes*/
	{0}
	/**ChildNodeInfoTypes*/
}
